package com.example.minaim;

import java.io.Serializable;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import org.apache.mina.core.filterchain.IoFilter;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.SocketConnector;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import android.R.integer;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.example.minaim.ClientHandler.Callback;
import com.example.minaim.Define.ClientStatus;
import com.example.minaim.MainActivity.MyReceiver;

public class tianfuActivity extends Activity {


	private Define define;
	
	private MyReceiver receiver;
	
	private User userInfo;
	//private List<BaseSkill> baseskills;
	private HashMap<String, BaseSkill> baseskills;
	private List<BaseSkill> baseSkillsList;
	
	public ClientStatus clientStatus = ClientStatus.idle;
	private String baseskill;
	
	private Button btnLearn;
	private Button btnCancel;
	private ListView listBaseskil;
	private TextView descTextView;
	
	private int Select = -1;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {

		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);//鍘绘帀鏍囬鏍�
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				  WindowManager.LayoutParams.FLAG_FULLSCREEN);//璁剧疆鍏ㄥ睆
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tianfu_main);

		Intent intent = this.getIntent();
		Bundle bundle = intent.getExtras();
		baseskills = (HashMap<String, BaseSkill>) bundle.getSerializable("allbaseskills");
		baseSkillsList = (List<BaseSkill>) bundle.getSerializable("baseSkillsList");
		baseskill = (String) bundle.getString("baseskill");
		
		receiver = new MyReceiver();                
		IntentFilter filter = new IntentFilter();		
		filter.addAction("com.minam.test");		
		tianfuActivity.this.registerReceiver(receiver, filter);	
		
		btnLearn = (Button) findViewById(R.id.btnTianfuLearn);
		btnCancel = (Button) findViewById(R.id.btnTianfuRetrun);
		listBaseskil = (ListView) findViewById(R.id.listtianfu);
		descTextView = (TextView) findViewById(R.id.txtDesc);		
		
		List<String> baseskillString = new ArrayList<String>();
		
		
		for (int x = 0; x < this.baseSkillsList.size(); x++) {
			baseskillString.add(this.baseSkillsList.get(x).get_name());
		}
		
		/*
		Iterator i = baseskills.entrySet().iterator();
		int x = 0;
		while(i.hasNext())
		{
			Entry entry = (Entry)i.next();
			//entry.getKey(); //返回与此项对应的键
			//entry.getValue(); //返回与此项对应的值
			BaseSkill aBaseSkill = (BaseSkill) entry.getValue();
		    baseskillString.add(aBaseSkill.get_name());
		}
		*/
		
		listBaseskil.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, baseskillString));
			
		this.listBaseskil.setOnItemClickListener(new OnItemClickListenerImpl()); // 单击选项
		/*
		
		btnLearn.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
		        String GRoleInfoCmd="GROLEINFO:";				
				
				Intent intent = new Intent();	
				intent.putExtra("cmd","send");
				intent.putExtra("body",GRoleInfoCmd);
				intent.setAction("com.minam.client");// action涓庢帴鏀跺櫒鐩稿悓		
				sendBroadcast(intent);
				clientStatus = ClientStatus.roleinfoing;
			}
		});
				*/
		btnCancel.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				finish();
			}
		});

	}
	
	private class OnItemClickListenerImpl implements OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			//String map = Integer.toString(arg2+1);
			//descTextView.setText(baseskills.get(map).get_desc());
			descTextView.setText(baseSkillsList.get(arg2).get_desc());
			Select = arg2;
		}
	}
	
	@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // 检测屏幕的方向：纵向或横向
        if (this.getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_LANDSCAPE) {
            //当前为横屏， 在此处添加额外的处理代码
        }
        else if (this.getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_PORTRAIT) {
            //当前为竖屏， 在此处添加额外的处理代码
        }
        //检测实体键盘的状态：推出或者合上
        if (newConfig.hardKeyboardHidden
                == Configuration.HARDKEYBOARDHIDDEN_NO){
            //实体键盘处于推出状态，在此处添加额外的处理代码
        }
        else if (newConfig.hardKeyboardHidden
                == Configuration.HARDKEYBOARDHIDDEN_YES){
            //实体键盘处于合上状态，在此处添加额外的处理代码
        }
    }
	
	public class MyReceiver extends BroadcastReceiver
    {
        
        // 鑷畾涔変竴涓箍鎾帴鏀跺櫒
        
        @Override
        public void onReceive(Context context, Intent intent)
        {
            
            // TODO Auto-generated method stub
            
            System.out.println("tianfuActivity OnReceiver");
            
            Bundle bundle = intent.getExtras();
            
            String cmd = bundle.getString("cmd");
            
            if (cmd.equals("receive")) {
            	String body = bundle.getString("body");
            	Cmd2vector cmd2vector = new Cmd2vector();
            	cmd2vector.cmd2vector(body);

            	if (cmd2vector.cmd.equals("c05")) {
					//String result = cmd2vector.paras.get("f");
            		//c05&total=2&number=1&name=濮濓箒鈧懎銇夌挧?desc=閹碘偓閺堝鏁鹃崙缁樺Η閼宠姤鏁鹃崙璇插婢х偛濮�%
            		
				}
            }

        }
        
        public MyReceiver()
        {
            
            System.out.println("MyReceiver");
            
            // 鏋勯�鍑芥暟锛屽仛涓�簺鍒濆鍖栧伐浣滐紝鏈緥涓棤浠讳綍浣滅敤
            
        }
        
    }
	
}
